﻿using Abp.Domain.Repositories;
using Camc.Quality.Dto.TableReturnDto.Technique;
using Camc.Quality.QualityAnalysis.RTechniqueEntity;
using Camc.Quality.QualityPlatform;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.Quality
{
    public class EditFileAppService : QualityAppServiceBase
    {
        private readonly IRepository<EditFileProcess, Guid> _editFileProcessRepository;
        private readonly IRepository<BackOperationDto, Guid> _backOperationDtoProcessRepository;
        private readonly IRepository<EditFileProcessInfo, Guid> _editFileProcessInfoRepository;
        private readonly IRepository<EditFileStepInfo, Guid> _editFileStepInfoRepository;
        private readonly IRepository<EditFileTableInfo, Guid> _editFileTableInfoRepository;
        private readonly IRepository<EditFileRowInfo, Guid> _editFileRowInfoRepository;
        private readonly IRepository<EditFileTableStructInfo, Guid> _editFileTableStructInfoRepository;
        private readonly IRepository<EditFileTableDataInfo, Guid> _editFileTableDataInfoRepository;
        private readonly IRepository<ClearSignatureRecord, Guid> _clearSignatureRecordRepository;
        private readonly IRepository<ProcessesEntity, Guid> _processesEntityRepository;
        private readonly IRepository<WorkStepEntity, Guid> _workStepEntityRepository;
        private readonly IRepository<RTechniqueEntity, Guid> _rtechniqueEntity;

        public EditFileAppService(

                                    IRepository<EditFileProcess, Guid> editFileProcessRepository,
                                    IRepository<BackOperationDto, Guid> backOperationDtoProcessRepository,
                                    IRepository<EditFileProcessInfo, Guid> editFileProcessInfoRepository,
                                    IRepository<EditFileStepInfo, Guid> editFileStepInfoRepository,
                                    IRepository<EditFileTableInfo, Guid> editFileTableInfoRepository,
                                    IRepository<EditFileRowInfo, Guid> editFileRowInfoRepository,
                                    IRepository<EditFileTableStructInfo, Guid> editFileTableStructInfoRepository,
                                    IRepository<EditFileTableDataInfo, Guid> editFileTableDataInfoRepository,
                                    IRepository<ClearSignatureRecord, Guid> clearSignatureRecordRepository,
                                    IRepository<ProcessesEntity, Guid> processesEntityRepository,
                                    IRepository<WorkStepEntity, Guid> workStepEntityRepository,
                                    IRepository<RTechniqueEntity, Guid> rtechniqueEntity
            )
        {
            _processesEntityRepository = processesEntityRepository;
            _workStepEntityRepository = workStepEntityRepository;
            _editFileProcessRepository = editFileProcessRepository;
            _backOperationDtoProcessRepository = backOperationDtoProcessRepository;
            _editFileProcessInfoRepository = editFileProcessInfoRepository;
            _editFileStepInfoRepository = editFileStepInfoRepository;
            _editFileTableInfoRepository = editFileTableInfoRepository;
            _editFileRowInfoRepository = editFileRowInfoRepository;
            _editFileTableStructInfoRepository = editFileTableStructInfoRepository;
            _editFileTableDataInfoRepository = editFileTableDataInfoRepository;
            _clearSignatureRecordRepository = clearSignatureRecordRepository;
            _rtechniqueEntity = rtechniqueEntity;
        }
        /// <summary>
        /// 获取靶场操作信息
        /// </summary>
        /// <returns></returns>
        public List<BackOperationDto> GetBCOperationData() 
        {
            return _backOperationDtoProcessRepository.GetAll().ToList();
        }
        /// <summary>
        /// 获取 日志信息
        /// </summary>
        /// <param name="guid">FileProcessSpecificationId</param>
        /// <returns></returns>
        public List<EditFileProcess> GetEditFileListByRtechId(Guid? guid)
        {
            if (guid != null)
            {
                var url = ConfigHelper.GetAppSetting("App", "GetAppUrl");
               var  returnData = _editFileProcessRepository.GetAll().AsNoTracking().Include(d => d.EditFileProcessInfos).ThenInclude(a => a.EditFileStepInfos)
                    .ThenInclude(c => c.EditFileTableInfos).ThenInclude(c => c.EditFileRowInfos).ThenInclude(d => d.EditFileTableDataInfos)
                  .Include(d => d.EditFileProcessInfos).ThenInclude(a => a.EditFileStepInfos).ThenInclude(c => c.EditFileTableInfos)
                  .ThenInclude(c => c.EditFileTableStructInfos).Where(d => d.RTechniqueEntityId == guid).OrderBy(c=>c._editVersion).ToList();
                string replaceData = "";
                foreach (var item in returnData)
                {
                    var techName = _rtechniqueEntity.FirstOrDefault(k => k.Id == item.RTechniqueEntityId);
                    foreach (var processEntity in item.EditFileProcessInfos)
                    {

                        if (processEntity.UpdateProcessesExplain.Contains("<img"))
                        {
                            replaceData = processEntity.UpdateProcessesExplain.Replace("src=\"", "src=\"" + url + "//UploadFile//" + (techName?.TechniqueNumber) + "//").Replace(".jepg", "_thumbnail.jepg");
                            processEntity.UpdateProcessesExplain = replaceData;
                        }
                        foreach (var stepInfos in processEntity.EditFileStepInfos)
                        {
                            if (stepInfos.WorkStepUpdateContent.Contains("<img"))
                            {
                                replaceData = stepInfos.WorkStepUpdateContent.Replace("src=\"","src=\"" +url+ "//UploadFile//"+(techName?.TechniqueNumber)+"//").Replace(".jepg","_thumbnail.jepg");
                                stepInfos.WorkStepUpdateContent = replaceData;
                            }
                        }
                    }
                }
                return returnData;                                       
            }
            else 
            {
                return new List<EditFileProcess>();
            }
        }
        /// <summary>
        /// 取消三检日志
        /// </summary>
        /// <param name="guid"></param> 
        /// <returns></returns>
        public List<ClearSignatureRecord> GetSignatureList(Guid? guid)
        {
            if (guid != null)
            {
                var techName = _rtechniqueEntity.FirstOrDefault(s=>s.Id == guid)?.TechniqueNumber;
                var clearSigatureRecord =  _clearSignatureRecordRepository.GetAll().Include(d => d.ClearSignatureRowDatas).Include(d => d.ClearSignatureDataStruces).Where(d => d.RTechniqueEntityId == guid).AsNoTracking().ToList();
                if(techName ==null) return new  List<ClearSignatureRecord>();
                foreach (var item in clearSigatureRecord)
                    {
                        foreach (var witem in item.ClearSignatureRowDatas.ToList())
                        {
                            if (witem.FieldName == "PhotoNo")
                            {
                                witem.Value = GetPictureRouteNew(witem.Value, techName, 0, true, false);
                            }
                        }
                        item.ClearSignatureDataStruces = item.ClearSignatureDataStruces.OrderBy(s => s.Index).ToList();
                    }
                return clearSigatureRecord;
            }
            else
            {
                return new List<ClearSignatureRecord>();
            }
        }
        private string GetPictureRouteNew(string picstr, string operationName, int type, bool isPaging = true, bool isExcel = false)
        {
            //处理多媒体图片的算法  存在格式 有jpeg  mp4
            if (isPaging == false && isExcel == false)
            {
                return picstr;
            }
            string newStr = "";
            string url = ConfigHelper.GetAppSetting("App", "GetAppUrl") + "/UploadFile/";
            if (!string.IsNullOrEmpty(picstr))
            {
                string distantRoute = "";
                var fileArr = picstr.Split(',');
                if (fileArr[0] == "否")
                {
                    return "否";
                }
                if (fileArr[0] == "是" && fileArr.Length == 1)
                {
                    return "";
                }
                else
                {
                    newStr = picstr.Replace("是", "");
                    fileArr = newStr.Split(',');
                }
                foreach (var item in fileArr)
                {
                    if (type == 0)
                    {

                        if (item.LastIndexOf("jpeg") != -1 || item.LastIndexOf("jpg") != -1)
                        {
                            string frontStr = "";
                            if (item.LastIndexOf("jpg") != -1)
                            {
                                frontStr = item.Replace(".jpg", "");
                                distantRoute = distantRoute + "," + url + operationName + @"/picture/" + frontStr + ".jpg";
                            }
                            else
                            {
                                frontStr = item.Replace(".jpeg", "");
                                distantRoute = distantRoute + "," + url + operationName + @"/picture/" + frontStr + ".jpeg";
                            }

                        }
                        else
                        {
                            distantRoute = distantRoute + "," + url + operationName + @"/video/" + item;
                        }

                    }
                    else
                    {
                        distantRoute = distantRoute + "," + url + operationName + @"/signature/" + item;
                    }
                }
                return distantRoute.Substring(1, distantRoute.Length - 1);
            }
            else
            {
                return "";
            }

        }
    } 
}
